package com.newasia.management.controller;/**
 * Created by mic on 2019/8/1.
 */

import com.newasia.commons.result.ResponseData;
import com.newasia.commons.result.ResponseUtil;
import com.newasia.management.IHomepageService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.dto.HomepageBanner.*;
import com.newasia.management.dto.HomepageItem.*;
import com.newasia.management.dto.HomepageProduct.*;
import com.newasia.management.dto.HomepageProductGroup.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * lyh
 * 2019/10/24
 */
@Slf4j
@RestController
@RequestMapping("/management")
public class HomepageController {

    @Reference(timeout = 3000)
    IHomepageService homepageService;


    /**轮播图**/
    /**增*/
    @PostMapping("/banner")
    public ResponseData createBanner(@RequestBody CreateBannerRequest request, HttpServletRequest httpServletRequest){

        CreateBannerResponse response= homepageService.createBanner(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }

    /**删*/
    @DeleteMapping("/banner/{bannerId}")
    public ResponseData deleteBanner(@PathVariable("bannerId") Long bannerId, HttpServletRequest httpServletRequest){

        DeleteBannerResponse response= homepageService.deleteBanner(bannerId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }


    /**改*/
    @PutMapping("/banner/{bannerId}")
    public ResponseData changeBrand(@PathVariable("bannerId") Long bannerId, @RequestBody ChangeBannerRequest request, HttpServletRequest httpServletRequest){

        ChangeBannerResponse response= homepageService.changeBanner(bannerId,request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

    /**查*/
    @GetMapping("/banner")
    public ResponseData banner(HttpServletRequest httpServletRequest){
        GetBannerResponse response= homepageService.getBannerList();

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }





    /**主页活动**/
    /**增*/
    @PostMapping("/homeItem")
    public ResponseData createHomeItem(@RequestBody CreateHomeItemRequest request, HttpServletRequest httpServletRequest){

        CreateHomeItemResponse response= homepageService.createHomeItem(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }

    /**删*/
    @DeleteMapping("/homeItem/{homeItemId}")
    public ResponseData deleteHomeItem(@PathVariable("homeItemId") Long homeItemId, HttpServletRequest httpServletRequest){

        DeleteHomeItemResponse response= homepageService.deleteHomeItem(homeItemId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }


    /**改*/
    @PutMapping("/homeItem/{homeItemId}")
    public ResponseData changeHomeItem(@PathVariable("homeItemId") Long homeItemId, @RequestBody ChangeBannerRequest request, HttpServletRequest httpServletRequest){

        ChangeHomeItemResponse response= homepageService.changeHomeItem(homeItemId,request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

    /**查*/
    @GetMapping("/homeItem")
    public ResponseData homeItem(HttpServletRequest httpServletRequest){
        GetHomeItemResponse response= homepageService.getHomeItemList();

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }
    /**主页商品组**/
    /**增*/
    @PostMapping("/homeProductGroup")
    public ResponseData createHomeProductGroup(@RequestBody CreateHomeProductGroupRequest request, HttpServletRequest httpServletRequest){

        CreateHomeProductGroupResponse response= homepageService.createHomeProductGroup(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }



    /**删*/
    @DeleteMapping("/homeProductGroup/{homeProductGroupId}")
    public ResponseData deleteHomeProductGroup(@PathVariable("homeProductGroupId") Long homeProductGroupId, HttpServletRequest httpServletRequest){

        DeleteHomeProductGroupResponse response= homepageService.deleteHomeProductGroup(homeProductGroupId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }


    /**改*/
    @PutMapping("/homeProductGroup/{homeProductGroupId}")
    public ResponseData changeHomeProductGroup(@PathVariable("homeProductGroupId") Long homeProductGroupId, @RequestBody ChangeHomeProductGroupRequest request, HttpServletRequest httpServletRequest){

        ChangeHomeProductGroupResponse response= homepageService.changeHomeProductGroup(homeProductGroupId,request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

    /**查*/
    @GetMapping("/homeProductGroup")
    public ResponseData homeProductGroup(HttpServletRequest httpServletRequest){
        GetHomeProductGroupResponse response= homepageService.getHomeProductGroupList();

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

    /**主页子项**/
    /**增*/
    @PostMapping("/homeProduct")
    public ResponseData createHomeProduct(@RequestBody CreateHomeProductRequest request, HttpServletRequest httpServletRequest){

        CreateHomeProductResponse response= homepageService.createHomeProduct(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }



    /**删*/
    @DeleteMapping("/homeProduct/{homeProductId}")
    public ResponseData deleteHomeProduct(@PathVariable("homeProductId") Long homeProductId, HttpServletRequest httpServletRequest){

        DeleteHomeProductResponse response= homepageService.deleteHomeProduct(homeProductId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }


    /**改*/
    @PutMapping("/homeProduct/{homeProductId}")
    public ResponseData changeHomeProduct(@PathVariable("homeProductId") Long homeProductId, @RequestBody ChangeHomeProductRequest request, HttpServletRequest httpServletRequest){

        ChangeHomeProductResponse response= homepageService.changeHomeProduct(homeProductId,request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

    /**查*/
    @GetMapping("/homeProduct")
    public ResponseData homeProduct(HttpServletRequest httpServletRequest){
        GetHomeProductResponse response= homepageService.getHomeProductList();

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

}
